package com.ningxun.rms.service.baserequire.impl;

import java.util.List;

import com.ningxun.rms.common.annotation.DataScope;
import com.ningxun.rms.common.utils.DateUtils;
import com.ningxun.rms.common.utils.SecurityUtils;
import com.ningxun.rms.domain.busrequire.BusRequire;
import com.ningxun.rms.mapper.BusRequireMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ningxun.rms.mapper.BusRequireApproveMapper;
import com.ningxun.rms.domain.busrequire.BusRequireApprove;
import com.ningxun.rms.service.baserequire.IBusRequireApproveService;
import org.springframework.transaction.annotation.Transactional;
import com.ningxun.rms.system.service.ISysDeptService;

import static com.ningxun.rms.common.utils.PageUtils.startPage;

/**
 * 岗位需求审核Service业务层处理
 * 
 * @author rms
 * @date 2025-07-15
 */
@Service
public class BusRequireApproveServiceImpl implements IBusRequireApproveService 
{
    @Autowired
    private BusRequireApproveMapper busRequireApproveMapper;

    @Autowired
    private BusRequireMapper busRequireMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询岗位需求审核
     * 
     * @param approveId 岗位需求审核主键
     * @return 岗位需求审核
     */
    @Override
    public BusRequireApprove selectBusRequireApproveByApproveId(Long approveId)
    {
        return busRequireApproveMapper.selectBusRequireApproveByApproveId(approveId);
    }

    /**
     * 查询岗位需求审核列表
     * 
     * @param busRequireApprove 岗位需求审核
     * @return 岗位需求审核
     */
    @DataScope(deptAlias = "r")
    @Override
    public List<BusRequireApprove> selectBusRequireApproveList(BusRequireApprove busRequireApprove)
    {
        busRequireApprove.setApproveStatus(1L);
        // 新增：支持地市筛选
        if (busRequireApprove.getDeptId() != null) {
            List<Long> deptIds = sysDeptService.selectDeptIdsByParentId(busRequireApprove.getDeptId());
            deptIds.add(busRequireApprove.getDeptId()); // 包含自身
            busRequireApprove.setDeptIds(deptIds);
        }
        startPage();
        return busRequireApproveMapper.selectBusRequireApproveList(busRequireApprove);
    }

    /**
     * 新增岗位需求审核
     * 
     * @param busRequireApprove 岗位需求审核
     * @return 结果
     */
    @Override
    public int insertBusRequireApprove(BusRequireApprove busRequireApprove)
    {
        return busRequireApproveMapper.insertBusRequireApprove(busRequireApprove);
    }

    /**
     * 修改岗位需求审核
     * 
     * @param busRequireApprove 岗位需求审核
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateBusRequireApprove(BusRequireApprove busRequireApprove)
    {
        Long requireId = busRequireApprove.getRequireId();
        BusRequire busRequire = busRequireMapper.selectBusRequireByRequireId(requireId);
        if(busRequire.getExpirationTime().before(DateUtils.getNowDate()) && busRequireApprove.getApproveStatus() == 0 ){
            throw new RuntimeException(
                    "截止时间已到，不能通过");
        }
        busRequire.setApproveState(busRequireApprove.getApproveStatus());
        if (busRequireApprove.getApproveStatus() == 0L) {
            busRequire.setRequireState(0L);
        }
        else {
            busRequire.setRequireState(1L);
        }
        busRequireMapper.updateBusRequire(busRequire);
        busRequireApprove.setApproveTime(DateUtils.getNowDate());
        busRequireApprove.setApproverId(SecurityUtils.getUserId());
        busRequireApprove.setApproveTime(DateUtils.getNowDate());
        return busRequireApproveMapper.updateBusRequireApprove(busRequireApprove);
    }

    /**
     * 批量删除岗位需求审核
     * 
     * @param approveIds 需要删除的岗位需求审核主键
     * @return 结果
     */
    @Override
    public int deleteBusRequireApproveByApproveIds(Long[] approveIds)
    {
        return busRequireApproveMapper.deleteBusRequireApproveByApproveIds(approveIds);
    }

    /**
     * 删除岗位需求审核信息
     * 
     * @param approveId 岗位需求审核主键
     * @return 结果
     */
    @Override
    public int deleteBusRequireApproveByApproveId(Long approveId)
    {
        return busRequireApproveMapper.deleteBusRequireApproveByApproveId(approveId);
    }
}
